#!/usr/bin/env lua

local function getVolumn()
  local f = io.popen("amixer get Master", "r")

  if not f then
    print("Fail to exec amixer")
    return
  end

  local output = f:read("*a")
  f:close()

  local Pattern = "(%d+)%%"
  local volumn = string.match(output, Pattern)

  return volumn
end

-- 生成进度条
local function generate_bar()
  local volumn = getVolumn()
  local bar_num = volumn / 5
  local bar = ""
  for _ = 1, bar_num do
    bar = bar.. "—"
  end
  return volumn, bar
end

local function main()

  if arg[1] ~= nil then
    local arg1 = arg[1]
    -- 根据参数增加或减小音量
    os.execute("amixer set Master 5%" .. arg1 .. ">/dev/null")
  end

  local volumn, bar = generate_bar()
  local command = string.format("dunstify -r 8777 '🕫  Vol: %s' %s", volumn, bar)
  os.execute(command)
end

main()
